package com.atdc.controller;

import com.atdc.entity.Memory;
import com.atdc.utill.Cal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

@Controller
public class MemoryController {
    @Autowired
    private Memory memory;
    private static int portname=1;
    private static int nextstart=0;


    @RequestMapping("/send")
//    @ResponseBody
    public String getFrom(HttpServletRequest request){

        int first = Integer.parseInt(request.getParameter("first"));
        int second = Integer.parseInt(request.getParameter("second"));
        int third = Integer.parseInt(request.getParameter("third"));
        int fourth = Integer.parseInt(request.getParameter("fourth"));
        String caltype = request.getParameter("caltype");
      switch (caltype){
          case "first":
              nextstart=Cal.FirstFit(memory.getSize(),first,second,third,fourth,portname);
              portname+=4;
              break;
          case "next":
              nextstart=Cal.NextFit(memory.getSize(),first,second,third,fourth,portname,nextstart);
              portname+=4;
              break;
          case "best":
              nextstart=Cal.BestFit(memory.getSize(),first,second,third,fourth,portname,nextstart);
              portname+=4;
              break;
          case "worst":
              nextstart=Cal.WorstFit(memory.getSize(),first,second,third,fourth,portname,nextstart);
              portname+=4;
              break;
          case "quick":
              nextstart=Cal.QuickFit(memory.getSize(),first,second,third,fourth,portname,nextstart);
              portname+=4;
              break;


      }

//        first+""+second+""+third+""+fourth;
        return "cal";

    }


    @RequestMapping("/torecycle")
    @ResponseBody
    public String getRecycle(HttpServletRequest request){

        int type = Integer.parseInt(request.getParameter("type"));
        for (int i = 0; i < memory.getSize().length; i++) {
            if(memory.getSize()[i]==type){
                memory.getSize()[i]=0;
            }
        }
        return "success";




//        first+""+second+""+third+""+fourth;


    }

    @RequestMapping("/set/{id}")
    public String changeMemory(@PathVariable Integer id){
        memory.getSize()[id]=99;
        return "index";

    }


    @RequestMapping(value = {"/index","/"})
    public String initMemory(Model model){
        int[] size = memory.getSize();
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < size.length; i++) {
            list.add(size[i]);
        }

        model.addAttribute("list", list);
        return  "index";
    }
    @RequestMapping("/cal")
    public String toCal(){

        return  "cal";
    }
    @RequestMapping("/recycle")
    public String toRecycle(){

        return  "recycle";
    }

}
